﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Globalization;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DLV.Data.Entities;
using DLV.FrameWork;
using DLV.Library.Utilities;

namespace DLV.BackEnd.Pages
{
    public partial class EditTravelGuide : AdminPageBase
    {
        protected string ThumbnailPreview = String.Empty;
        protected List<LanguageInfo> LanguageList = new List<LanguageInfo>(); 

        private TravelGuideInfo _travelGuideInfo = new TravelGuideInfo();
        private readonly TravelGuideController _travelGuideController = new TravelGuideController();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!String.IsNullOrEmpty(Request.QueryString["TravelGuideId"]))
                _travelGuideInfo = _travelGuideController.GetTravelGuide(Int32.Parse(Request.QueryString["TravelGuideId"]));

            if (Page.IsPostBack) return;

            LoadLanguage();
            LoadGender();
            LoadLevel();
            LoadTravelType();
            LoadContinent();
            LoadAddressDefault();
            LoadAdminUser();
            LoadStatus();

            if (_travelGuideInfo.TravelGuideId == -1) return;

            LoadNational();
            LoadCity();
            LoadDistrict();
            LoadWard();

            RenderForm();
        }

        protected void lbtSave_Click(object sender, EventArgs e)
        {
            SaveData();
        }
        protected void lbtClose_Click(object sender, EventArgs e)
        {
            Response.Redirect("/Pages/ListTravelGuide.aspx?xml=TravelGuide");
        }
        private void SaveData()
        {
            _travelGuideInfo.FullName = txtFullName.Text;
            _travelGuideInfo.Avatar = thumbnailAvatar.Value;
            _travelGuideInfo.Info = txtContent.Text;
            _travelGuideInfo.Birthday = String.IsNullOrEmpty(dteBirthday.Value)
                ? Null.NullDate
                : Convert.ToDateTime(dteBirthday.Value);
            _travelGuideInfo.IdentityCard = txtIdentityCard.Text;
            _travelGuideInfo.Gender = ddlGender.SelectedValue;
            _travelGuideInfo.Phone = txtPhone.Text;
            _travelGuideInfo.Email = txtEmail.Text;
            _travelGuideInfo.Facebook = txtFacebook.Text;
            _travelGuideInfo.GooglePlus = txtGooglePlus.Text;
            _travelGuideInfo.Twitter = txtTwitter.Text;
            _travelGuideInfo.Level = Convert.ToInt32(ddlLevel.SelectedValue);
            _travelGuideInfo.ContinentId = Convert.ToInt32(ddlContinent.SelectedValue);
            _travelGuideInfo.NationalId = Convert.ToInt32(Request["ctl00$placeHolderContent$ddlNational"]);
            _travelGuideInfo.CityId = Convert.ToInt32(Request["ctl00$placeHolderContent$ddlCity"]);
            _travelGuideInfo.DistrictId = Convert.ToInt32(Request["ctl00$placeHolderContent$ddlDistrict"]);
            _travelGuideInfo.WardId = Convert.ToInt32(Request["ctl00$placeHolderContent$ddlWard"]);
            _travelGuideInfo.Address = txtAddress.Text;
            _travelGuideInfo.Languages = txtLanguages.Value;
            _travelGuideInfo.Status = int.Parse(ddlStatus.SelectedValue);

            if (_travelGuideInfo.TravelGuideId == -1)
            {
                _travelGuideInfo.CreatedBy = ddlAdminUser.SelectedValue == "-1"
                    ? UserId
                    : int.Parse(ddlAdminUser.SelectedValue);
                _travelGuideInfo.CreatedDate = DateTime.Now;
                _travelGuideController.InsertTravelGuide(_travelGuideInfo);
            }
            else
            {
                _travelGuideInfo.UpdatedBy = UserId;
                _travelGuideInfo.UpdatedDate = DateTime.Now;
                _travelGuideController.UpdateTravelGuide(_travelGuideInfo);
            }

            Response.Redirect("/Pages/ListTravelGuide.aspx?xml=TravelGuide");
        }
        private void RenderForm()
        {
            txtFullName.Text = _travelGuideInfo.FullName;

            ThumbnailPreview = String.Format("<img src='{0}' />", ImageUpload.GetImageUrl(200, _travelGuideInfo.Avatar, false, ""));
            thumbnailAvatar.Value = _travelGuideInfo.Avatar;

            txtContent.Text = _travelGuideInfo.Info;
            dteBirthday.Value = _travelGuideInfo.Birthday.ToString("dd/MM/yyyy");
            txtIdentityCard.Text = _travelGuideInfo.IdentityCard;
            ddlGender.SelectedValue = _travelGuideInfo.Gender;
            txtPhone.Text = _travelGuideInfo.Phone;
            txtEmail.Text = _travelGuideInfo.Email;
            ddlLevel.SelectedValue = _travelGuideInfo.Level.ToString();
            ddlContinent.SelectedValue = _travelGuideInfo.ContinentId.ToString();
            ddlNational.SelectedValue = _travelGuideInfo.NationalId.ToString();
            ddlCity.SelectedValue = _travelGuideInfo.CityId.ToString();
            ddlDistrict.SelectedValue = _travelGuideInfo.DistrictId.ToString();
            ddlWard.SelectedValue = _travelGuideInfo.WardId.ToString();
            txtAddress.Text = _travelGuideInfo.Address;
            txtLanguages.Value = _travelGuideInfo.Languages;
            txtFacebook.Text = _travelGuideInfo.Facebook;
            txtGooglePlus.Text = _travelGuideInfo.GooglePlus;
            txtTwitter.Text = _travelGuideInfo.Twitter;
            ddlAdminUser.SelectedValue = _travelGuideInfo.CreatedBy.ToString();
            ddlStatus.SelectedValue = _travelGuideInfo.Status.ToString();
        }
        private void LoadLanguage()
        {
            LanguageList = new LanguageController().ListLanguage();
        }
        private void LoadAddressDefault()
        {
            ddlNational.Items.Insert(0, new ListItem("--- Chọn quốc gia ---", "-1"));
            ddlCity.Items.Insert(0, new ListItem("--- Chọn tỉnh/thành phố ---", "-1"));
            ddlDistrict.Items.Insert(0, new ListItem("--- Chọn quận/huyện ---", "-1"));
            ddlWard.Items.Insert(0, new ListItem("--- Chọn phường/xã ---", "-1"));
        }
        private void LoadGender()
        {
            ddlGender.Items.Insert(0, new ListItem("Nam", "Nam"));
            ddlGender.Items.Insert(1, new ListItem("Nữ", "Nữ"));
        }
        private void LoadLevel()
        {
            var ds = new DataSet();
            ds.ReadXml(MapPath("~/xml/Level.xml"));
            var dv = ds.Tables["Level"].DefaultView;
            ddlLevel.DataTextField = "Name";
            ddlLevel.DataValueField = "ID";
            ddlLevel.DataSource = dv;
            ddlLevel.DataBind();

            ddlLevel.Items.Insert(0, new ListItem("--- Chọn trình độ ---", "-1"));
            
        }
        private void LoadTravelType()
        {
            ddlTravelType.Items.Insert(0, new ListItem("--- Chọn loại tuyến ---", ""));
            ddlTravelType.Items.Insert(1, new ListItem("Một tuyến", "Một tuyến"));
            ddlTravelType.Items.Insert(2, new ListItem("Nhiều tuyến", "Nhiều tuyến"));
            ddlTravelType.Items.Insert(3, new ListItem("Suốt tuyến", "Suốt tuyến"));
        }
        private void LoadContinent()
        {
            var ds = new DataSet();
            ds.ReadXml(MapPath("~/xml/Continent.xml"));
            var dv = ds.Tables["Continent"].DefaultView;
            ddlContinent.DataTextField = "Name";
            ddlContinent.DataValueField = "ID";
            ddlContinent.DataSource = dv;
            ddlContinent.DataBind();

            ddlContinent.Items.Insert(0, new ListItem("--- Chọn châu lục ---", "-1"));
        }
        private void LoadNational()
        {
            ddlNational.DataSource = new NationalController().ListNationalByContinent(_travelGuideInfo.ContinentId);
            ddlNational.DataTextField = "NationalName";
            ddlNational.DataValueField = "NationalId";
            ddlNational.DataBind();

            ddlNational.Items.Insert(0, new ListItem("--- Chọn quốc gia ---", "-1"));
        }
        private void LoadCity()
        {
            ddlCity.DataSource = new CityController().ListCityByNational(_travelGuideInfo.NationalId);
            ddlCity.DataTextField = "CityName";
            ddlCity.DataValueField = "CityId";
            ddlCity.DataBind();

            ddlCity.Items.Insert(0, new ListItem("--- Chọn tỉnh/thành phố ---", "-1"));
        }
        private void LoadDistrict()
        {
            ddlDistrict.DataSource = new DistrictController().ListDistrictByCity(_travelGuideInfo.CityId);
            ddlDistrict.DataTextField = "DistrictName";
            ddlDistrict.DataValueField = "DistrictId";
            ddlDistrict.DataBind();

            ddlDistrict.Items.Insert(0, new ListItem("--- Chọn quận/huyện ---", "-1"));
        }
        private void LoadWard()
        {
            ddlWard.DataSource = new WardController().ListWardByDistrict(_travelGuideInfo.DistrictId);
            ddlWard.DataTextField = "WardName";
            ddlWard.DataValueField = "WardId";
            ddlWard.DataBind();

            ddlWard.Items.Insert(0, new ListItem("--- Chọn phường/xã ---", "-1"));
        }

        private void LoadAdminUser()
        {
            ddlAdminUser.DataSource = new AdminUserController().ListAdminUser();
            ddlAdminUser.DataTextField = "Username";
            ddlAdminUser.DataValueField = "UserID";
            ddlAdminUser.DataBind();

            ddlAdminUser.Items.Insert(0, new ListItem("--- Chọn người tạo ---", "-1"));
        }

        private void LoadStatus()
        {
            var ds = new DataSet();
            ds.ReadXml(MapPath("~/xml/Status.xml"));
            var dv = ds.Tables["Status"].DefaultView;
            ddlStatus.DataTextField = "Name";
            ddlStatus.DataValueField = "ID";
            ddlStatus.DataSource = dv;
            ddlStatus.DataBind();
        }
    }
}